<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores'
import { message } from '@/components/ZyMessage/index.js'

const router = useRouter()
const userStore = useUserStore()

// 登录
const loading = ref(false)
const params = ref({ username: '', password: '' })
const onSubmit = async () => {
  if (!params.value.username || !params.value.password) {
    return message('用户名或密码不能为空', 'error')
  }
  loading.value = true
  try {
    await userStore.login(params.value)
    router.replace('/')
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <!-- 顶部 -->
    <div class="head">
      <img class="logo" src="@/assets/images/logo.svg" alt="" />
      <span class="title">知语拾光</span>
    </div>
    <!-- main -->
    <div class="main">
      <p class="title">账号登录</p>
      <div class="item">
        <div class="label">用户名</div>
        <input class="input" v-model="params.username" type="text" placeholder="用户名" />
      </div>
      <div class="item">
        <div class="label">密码</div>
        <input class="input" v-model="params.password" type="password" placeholder="密码" />
      </div>
      <!-- 提交 -->
      <zy-button class="submit" :loading="loading" @click="onSubmit">登录</zy-button>
      <!-- 返回首页 -->
      <div class="bottom">
        <p class="return" @click="$router.push('/')">返回首页</p>
        <p @click="$router.push('/register')">注册</p>
      </div>
    </div>
    <!-- 提示 -->
    <p class="tip">
      该注册账号只限于在网站内测试、学习，请妥善保护自己个人隐私和个人信息，以免造成不必要的影响。谢谢～
    </p>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  min-height: 100vh;
  background-color: #f4f5f7;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.head {
  display: flex;
  align-items: center;
  .logo {
    width: 45px;
  }
  .title {
    font-size: 32px;
    color: #202020;
    font-weight: 600;
    margin-left: 8px;
  }
}
.main {
  width: 400px;
  background-color: #ffffff;
  border-radius: 8px;
  padding: 40px;
  margin-top: 30px;
  .title {
    font-size: 20px;
    font-weight: 600;
    line-height: 28px;
    margin-bottom: 30px;
  }
  .item {
    margin-bottom: 20px;
    .label {
      margin-bottom: 8px;
    }
    .input {
      height: 48px;
      width: 100%;
      padding: 0 16px;
      border: none;
      border-radius: 8px;
      background-color: #f6f6f8;
      outline: none;
    }
  }
  .submit {
    margin-top: 10px;
    width: 100%;
    background-color: #202020;
    height: 48px;
    font-size: 16px;
    border-radius: 8px;
  }
  .bottom {
    margin-top: 12px;
    display: flex;
    justify-content: space-between;
    p {
      color: #1890ff;
      cursor: pointer;
    }
    .return {
      color: #949494;
    }
  }
}

.tip {
  margin-top: 40px;
  text-align: center;
  color: #949494;
}
</style>
